package com.example.yy.fragementtest318yy.adapter;

import android.content.Context;
import android.content.Intent;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;

import com.example.yy.fragementtest318yy.R;
import com.example.yy.fragementtest318yy.activity.ViewVideoActivity;
import com.example.yy.fragementtest318yy.bean.VideoBean;
import com.squareup.picasso.Picasso;

import java.util.List;

/**
 * Created by yy on 2017/4/25.
 */

public class VideoAdapter extends RecyclerView.Adapter{
    private Context context;
    private LayoutInflater layoutInflater;//动态加载布局
    private List<VideoBean> list;

    public VideoAdapter(Context context){
        super();
        this.context=context;
        layoutInflater=LayoutInflater.from(context);
    }
    public void setList(List<VideoBean>list){
        this.list=list;
    }
    public class ViewHolder extends RecyclerView.ViewHolder{

        private ImageView iv;
        private TextView tvtitle,tvdescript,tvtime;
        private Button btn;

        public ViewHolder(View itemView) {
            super(itemView);
            iv=(ImageView)itemView.findViewById(R.id.imageView);
            tvtitle=(TextView)itemView.findViewById(R.id.textView);
            tvdescript=(TextView)itemView.findViewById(R.id.textView2);
            tvtime=(TextView)itemView.findViewById(R.id.textView3);
            btn=(Button)itemView.findViewById(R.id.button);
        }
    }


    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View v = LayoutInflater.from(parent.getContext()).inflate(R.layout.item, parent, false);
        return new ViewHolder(v);
    }

    @Override
    public void onBindViewHolder(RecyclerView.ViewHolder holder, final int position) {
        VideoBean videoBean=list.get(position);
        if(videoBean==null)
            return;
        ViewHolder viewHolder=(ViewHolder)holder;
        //viewHolder.iv.setImageResource(twareBean.getImgid());
        viewHolder.tvtitle.setText(videoBean.getName());
        viewHolder.tvdescript.setText(videoBean.getDescription());
        viewHolder.tvtime.setText(videoBean.getUpdate_time());
        Picasso.with(context).load("http://amicool.neusoft.edu.cn/Uploads/" + videoBean.getThumb()).placeholder(R.mipmap.ic_launcher).into(viewHolder.iv);
        viewHolder.btn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                list.remove(position);
                notifyDataSetChanged();//更新RecyclerView
                //在实际案例中要删除应该会涉及网络或数据库访问
                //一般要有删除前的提醒
            }
        });
        viewHolder.itemView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                //获取当前条目信息，如id
                int id=list.get(position).getId();

                //Toast.makeText(context,""+id,Toast.LENGTH_SHORT).show();
                //实际中点击后打开新窗口做其他操作
                Intent intent = new Intent(context, ViewVideoActivity.class);
                intent.putExtra("videopath",list.get(position).getVideopath());
                context.startActivity(intent);
            }
        });

    }

    @Override
    public int getItemCount() {
        if (list == null) {
            return 0;
        } else
            return list.size();
    }
}
